package com.atguigu.spzx.order.controller;

import com.atguigu.spzx.common.result.Result;
import com.atguigu.spzx.common.result.ResultCodeEnum;
import com.atguigu.spzx.common.utils.PageUtils;
import com.atguigu.spzx.order.entity.OrderInfo;
import com.atguigu.spzx.order.service.OrderInfoService;
import com.atguigu.spzx.order.vo.OrderInfoDetailRespVo;
import com.atguigu.spzx.order.vo.OrderInfoQueryRespVo;
import com.atguigu.spzx.order.vo.OrderInfoSaveReqVo;
import com.atguigu.spzx.order.vo.TradeRespVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * OrderInfoController
 * </p>
 *
 * @author flyduck
 * @since 2023/11/6
 */
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation("获取订单详情")
    @GetMapping("/auth/{orderId}")
    public Result getOrderInfoById(@PathVariable Long orderId){
        OrderInfoDetailRespVo respVo = orderInfoService.getOrderInfoById(orderId);
        return Result.ok(respVo);
    }

    @ApiOperation("提交订单")
    @PostMapping("/auth/submitOrder")
    public Result<Long> submitOrder(@RequestBody OrderInfoSaveReqVo vo) {
        Long orderId = orderInfoService.submitOrder(vo);
        return Result.ok(orderId);
    }

    @ApiOperation("确认下单")
    @GetMapping("/auth/trade")
    public Result<TradeRespVo> trade(){
        TradeRespVo tradeRespVo = orderInfoService.getTrade();
        return Result.ok(tradeRespVo);
    }

    @ApiOperation("立即购买")
    @GetMapping("auth/buy/{skuId}")
    public Result<TradeRespVo> buy(@PathVariable Long skuId) {
        TradeRespVo tradeVo = orderInfoService.buy(skuId);
        return Result.ok(tradeVo);
    }

    @ApiOperation("获取订单分页列表")
    @GetMapping("auth/{page}/{limit}")
    public Result<PageInfo<OrderInfoQueryRespVo>> list(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            @RequestParam(required = false, defaultValue = "") Integer orderStatus) {
        Page<OrderInfo> pageParam = new Page<>(page,limit);
        Page<OrderInfoQueryRespVo> pageInfo = orderInfoService.getOrderInfoPageByOrderStatus(pageParam, orderStatus);
        return Result.ok(PageUtils.convertToPageInfo(pageInfo));
    }
}
